package com.aisino.nmg.ant;

import com.aisino.nmg.ant.project.IProject;
import com.aisino.nmg.ant.task.EnvironmentTask;
import com.aisino.nmg.ant.task.GeneratorToolTask;
import com.aisino.nmg.config.GeneratorProperties;
import com.aisino.nmg.system.generator.model.ColumnConfig;
import com.aisino.nmg.util.SpringContextUtil;

import java.util.Iterator;
import java.util.List;

public class GeneratorUtil {
	public static GeneratorToolTask getGeneraterToolTask(IProject project) {
		GeneratorProperties generatorProperties = SpringContextUtil.getBean(GeneratorProperties.class);
		if(generatorProperties == null){
			throw new RuntimeException("加载生成器配置属性失败");
		}
		GeneratorToolTask generaterToolTask = new GeneratorToolTask(project);
		EnvironmentTask environmentTask = generaterToolTask.createEnvironment();
		environmentTask.setGeneratorProperties(generatorProperties);

		return generaterToolTask;
	}

	public static ColumnConfig getPrimaryKey(List<ColumnConfig> columnConfigs){
		Iterator<ColumnConfig> it = columnConfigs.iterator();
		while (it.hasNext()) {
			ColumnConfig columnConfig = it.next();
			if(columnConfig.getIsPrimaryKey().equalsIgnoreCase("1")){
				return columnConfig;
			}
		}
		// TODO 主键为空的情况
		return null;
	}
}
